//
//  HMEmotionPackage.swift
//  EmotionKeybaord
//
//  Created by ZhuDong on 16/5/31.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

let pageCount = 21
class HMEmotionPackage: NSObject {
    var title: String?
    var sectionEmotions:[[HMEmotion]] = [[HMEmotion]]()
    init(id: String, title: String,emotions:[[String:AnyObject]]){
        super.init()
        self.title = title
        var tempArr = [HMEmotion]()
        var index = 0
        for item in emotions{
            let emotion = HMEmotion(id: id, dict: item)
            
            tempArr.append(emotion)
            index += 1
            if index == (pageCount - 1) {
                let delete = HMEmotion(isDelete: true)
                tempArr.append(delete)
                index = 0
            }
        }
        let delta = tempArr.count % pageCount
        if delta != 0 {
            for _ in delta..<(pageCount - 1) {
                let emotion = HMEmotion(isEmpty:true)
                tempArr.append(emotion)
            }
            let delete = HMEmotion(isDelete: true)
            tempArr.append(delete)
        }
        getSectionEmotions(tempArr)
    }
    func getSectionEmotions(array: [HMEmotion]){
        var sectionEmotions:[[HMEmotion]] = [[HMEmotion]]()
        let sectionCount = (array.count - 1) / pageCount + 1
        for i in 0..<sectionCount {
            let loction = i * pageCount
            var length = pageCount
            if (loction + length) > array.count {
                length = array.count - loction
            }
            let range = NSRange(location: loction, length: length)
            let section = (array as NSArray).subarrayWithRange(range)
            sectionEmotions.append(section as! [HMEmotion])
        }
        self.sectionEmotions = sectionEmotions
    }
}
